// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Bible.java

package presenter;

import java.io.*;
import java.util.ArrayList;

// Referenced classes of package presenter:
//            BibleBook, SongVerse

public class Bible
{
    private BufferedReader br;
    String name="";
    static String booksOfOT[] = {
        "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy", "Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel", 
        "1 Kings", "2 Kings", "1 Chronicles", "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job", "Psalms", "Proverbs", 
        "Ecclesiastes", "Song of Solomon", "Isaiah", "Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos", 
        "Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai", "Zechariah", "Malachi"
    };
    String booksOfNT[] = {
        "Matthew", "Mark", "Luke", "John", "Acts", "Romans", "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians", 
        "Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians", "1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James", 
        "1 Peter", "2 Peter", "1 John", "2 John", "3 John", "Jude", "Revelation"
    };
    String allBooks[];
    private BibleBook oldT[];
    private BibleBook newT[];
    String numbers;

    public Bible(File path, String name)
    {
        numbers = "0123456789";
        this.name=name.substring(0,name.indexOf(".xmm"));
        try
        {
            oldT = new BibleBook[booksOfOT.length];
            newT = new BibleBook[booksOfNT.length];
            br = new BufferedReader(new FileReader(path));
            //oldT = getTestament(oldT, booksOfOT);
            newT[0] = new BibleBook("Matthew");
            //newT = getTestament(newT, booksOfNT);
            readXML();
            allBooks = new String[booksOfOT.length + booksOfNT.length];
            for(int i = 0; i < booksOfOT.length; i++)
                allBooks[i] = booksOfOT[i];

            for(int i = 0; i < booksOfNT.length; i++)
                allBooks[i + booksOfOT.length] = booksOfNT[i];

        }
        catch(Exception err)
        {
            err.printStackTrace();
        }
    }

    private BibleBook[] getTestament(BibleBook testament[], String booksOfTestament[])
    {
        int index = 0;
        int verse = 0;
        int chapter = 0;
        String x = "";
        ArrayList<String> currentVerse = new ArrayList<String>();
        testament[0] = new BibleBook(booksOfTestament[0]);
        do
        {
            do
            {
                try
                {
                    x = br.readLine();
                }
                catch(Exception e) { }
                if(x != null && x.equals("Matthew"))
                    return testament;
                if(x != null && index + 1 < booksOfTestament.length && x.equals(booksOfTestament[index + 1]))
                {
                    index++;
                    testament[index] = new BibleBook(booksOfTestament[index]);
                    chapter = 0;
                    verse = 0;
                    continue;
                }
                if(x != null && !x.equals("") && x.contains(":") && numbers.contains(x.substring(0, 1)))
                {
                    currentVerse = new ArrayList<String>();
                    String chap = x.substring(0, 1);
                    int start = 0;
                    int i = 1;
                    do
                    {
                        if(i >= x.length())
                            break;
                        if(numbers.contains(x.substring(i, i + 1)))
                        {
                            chap = (new StringBuilder()).append(chap).append(x.substring(i, i + 1)).toString();
                        } else
                        {
                            chapter = Integer.parseInt(chap);
                            start = i + 1;
                            break;
                        }
                        if(i == x.length() - 1)
                        {
                            chapter = Integer.parseInt(chap);
                            start = i + 1;
                            break;
                        }
                        i++;
                    } while(true);
                    String vers = "";
                    int init = start;
                    do
                    {
                        if(init >= x.length())
                            break;
                        if(numbers.contains(x.substring(init, init+ 1)))
                        {
                            vers = (new StringBuilder()).append(vers).append(x.substring(init, init + 1)).toString();
                        } else
                        {
                            verse = Integer.parseInt(vers);
                            start = init + 1;
                            break;
                        }
                        if(init == x.length() - 1)
                        {
                            verse = Integer.parseInt(vers);
                            start = init + 1;
                            break;
                        }
                        init++;
                    } while(true);
                    currentVerse.add(x.substring(start));
                    continue;
                }
                if(x == null || x.equals(""))
                    break;
                currentVerse.add(x);
            } while(true);
            if(chapter > 0)
                testament[index].addVerse(chapter, verse, (String[])currentVerse.toArray(new String[1]));
        } while(x != null);
        return testament;
    }

    public SongVerse[] getVerses(String book, int chapterStart, int verseStart, int chapterEnd, int verseEnd)
    {
        for(int i = 0; i < booksOfOT.length; i++)
            if(book.equals(booksOfOT[i]))
                return oldT[i].getVerses(chapterStart, verseStart, chapterEnd, verseEnd);

        for(int i = 0; i < booksOfNT.length; i++)
            if(book.equals(booksOfNT[i]))
                return newT[i].getVerses(chapterStart, verseStart, chapterEnd, verseEnd);

        return new SongVerse[1];
    }

    public SongVerse[] getVerses(String input)
    {
        System.out.println("test");
        String book = "";
        int chapterStart = 0;
        int verseStart = 0;
        int chapterEnd = 0;
        int verseEnd = 0;
        int index = 0;
        int i = 0;
        do
        {
            if(i >= input.length())
                break;
            if(numbers.contains(input.substring(i, i + 1)))
            {
                book = input.substring(0, i);
                index = i;
                break;
            }
            i++;
        } while(true);
        System.out.println(book);
        i = index;
        do
        {
            if(i >= input.length())
                break;
            if(input.substring(i, i + 1).equals(":"))
            {
                chapterStart = Integer.parseInt(input.substring(index, i));
                index = i + 1;
                break;
            }
            i++;
        } while(true);
        i = index;
        do
        {
            if(i >= input.length())
                break;
            if(input.substring(i, i + 1).equals("-"))
            {
                verseStart = Integer.parseInt(input.substring(index, i));
                index = i + 1;
                break;
            }
            i++;
        } while(true);
        i = index;
        do
        {
            if(i >= input.length())
                break;
            if(input.substring(i, i + 1).equals(":"))
            {
                chapterEnd = Integer.parseInt(input.substring(index, i));
                index = i + 1;
                break;
            }
            i++;
        } while(true);
        verseEnd = Integer.parseInt(input.substring(index, input.length()));
        System.out.println(book+" "+chapterStart+" "+verseStart+" "+chapterEnd+" "+verseEnd);
        return getVerses(book, chapterStart-1, verseStart-1, chapterEnd-1, verseEnd-1);
    }
    
    public BibleBook[] getXMLTestament(BibleBook[] testament) {
        
        
        return testament;
    }
    
    public void readXML() {
        try {
            String in=br.readLine();
            while(!in.startsWith("<b n=")) {
                in=br.readLine();
                
            }
            int chapter=1;
            int verse=1;
            int book=0;
            boolean old=true;
            while(!in.contains("</bible>")) {
                String title=in.substring(in.indexOf("\"")+1,in.lastIndexOf("\""));
                //System.out.println(title);
                if(title.equals("Matthew")) {
                    old=false;
                    book=0;
                }
                if(old) 
                    oldT[book]=new BibleBook(title);
                else
                    newT[book]=new BibleBook(title);
                book++;
                in=br.readLine();
                while(!in.contains("<b n=")) {
                    if(in.contains("/bible"))
                        return;
                    if(in.startsWith("<c n=")) {
                        chapter=Integer.parseInt(in.substring(in.indexOf("\"")+1,in.indexOf("\">")));
                    } else if(in.startsWith("<v n=")) {
                        verse=Integer.parseInt(in.substring(in.indexOf("\"")+1,in.indexOf("\">")));
                        ArrayList<String> verseText=new ArrayList<String>();
                        if(in.contains("</v>")) {
                            verseText.add(in.substring(in.indexOf(">")+1,in.lastIndexOf("<")));
                        } else {                            
                            while(!in.contains("</v>")) {
                                verseText.add(in.substring(in.indexOf(">")+1));
                            }
                            verseText.add(in.substring(0, in.indexOf("</v>")));
                        }
                        if(old) {
                            oldT[book-1].addVerse(chapter, verse, verseText.toArray(new String[0]));
                        } else {
                            newT[book-1].addVerse(chapter, verse, verseText.toArray(new String[0]));
                        }
                    }
                    in=br.readLine();
                }
                    
                
            }
            System.out.println("finished reading");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public BibleBook getBook(String bookName)
    {
        for(int i = 0; i < oldT.length; i++)
            if(bookName.equals(oldT[i].getName()))
                return oldT[i];

        for(int i = 0; i < newT.length; i++)
            if(bookName.equals(newT[i].getName()))
                return newT[i];

        return null;
    }

    public static void main(String args[]) {
        Bible b = new Bible(new File("Bibles/KJV.xmm"),"KJV");
    }

    

}
